#include<bits/stdc++.h>

using namespace std;
using ll = long long;
#define endl '\n'

void print(vector<int> a) {
    copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    for (int n; cin >> n;) {
        vector<int> a(n);
        for (auto &e : a) {
            cin >> e;
        }
        vector<int> b(a), c(a);
        prev_permutation(b.begin(), b.end());
        next_permutation(c.begin(), c.end());
        if (b < a) {
            print(b);
        }
        print(a);
        if (c > a) {
            print(c);
        }
    }
    return 0;
}